package com.pubinfo.passbook.data.controller.forward;

import com.alibaba.fastjson.JSONObject;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.common.config.ApplicationConfig;
import com.pubinfo.passbook.common.entity.TFileEnclosureFtp;
import com.pubinfo.passbook.common.mapper.TWiteoffInstanceMapper;
import com.pubinfo.passbook.common.model.http.GetReimbursementInformation.ReimbursementInformation;
import com.pubinfo.passbook.common.model.http.GetReimbursementInformation.WriteoffInstanceCode;
import com.pubinfo.passbook.common.model.http.request.GainWriteoffInstStatus.GainWriteoffInstStatusItems;
import com.pubinfo.passbook.common.model.http.response.GetWriteoffInstStatus.WriteoffInstStatusMessage;
import com.pubinfo.passbook.common.model.ruler.Reimbursement;
import com.pubinfo.passbook.common.model.ruler.ReimbursementParam;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.mss.HttpService;
import com.pubinfo.passbook.common.service.mss.WriteoffInstanceService;
import com.pubinfo.passbook.common.service.ruler.RuleWenZhouService;
import com.pubinfo.passbook.common.service.uccp.UccpService;
import com.pubinfo.passbook.data.service.CreateExcelService;
import com.pubinfo.passbook.data.service.MssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping(value = "mss")
@Api(tags = "财辅相关接口")
@Slf4j
public class MssController {
  @Autowired
  WriteoffInstanceService writeoffInstanceService;
  @Autowired
  CreateExcelService createExcelService;
  @Resource
  ApplicationConfig config;
  @Autowired
  UccpService uccpService;
  @Autowired
  RuleWenZhouService wenZhouService;
  @Autowired
  MssService mssService;
  @Autowired
  HttpService httpService;
  @Autowired
  TWiteoffInstanceMapper tWiteoffInstanceMapper;
  
  @PostMapping("getReimbursementInformationByDate/{date}")
  @ApiOperation(value = "通过日期获取报账单状态信息")
  public JsonResult<ReimbursementInformation> getReimbursementInformationByDate(@PathVariable String date) throws UnirestException, ParseException {
    return JsonResult.success(writeoffInstanceService.getReimbursementInformationByDate(date));
  }
  
  @PostMapping("getReimbursementInformationByWriteoffInstanceCode")
  @ApiOperation(value = "通过报账单号获取报账单状态信息")
  public JsonResult<ReimbursementInformation> getReimbursementInformationByWriteoffInstanceCode(@RequestBody List<WriteoffInstanceCode> list) throws UnirestException, ParseException {
    return JsonResult.success(writeoffInstanceService.getReimbursementInformationByWriteoffInstanceCode(list));
  }
  
  @PostMapping("getWriteoffBaseDataItem")
  @ApiOperation(value = "获取报账组织信息")
  public String getWriteoffInstStatusBodyItem(@RequestBody String xml) {
    String postUrl = config.getEsb().getUrl() + config.getEsb().getApi().getGetWriteoffBaseData();
    return mssService.sendPost(xml, postUrl);
  }
  
  @PostMapping("getSapNumber")
  @ApiOperation(value = "获取sap凭证")
  public String getSapNumber(@RequestBody String xml) {
    String postUrl = config.getEsb().getUrl() + config.getEsb().getApi().getGetSapNumber();
    return mssService.sendPost(xml, postUrl);
  }
  
  @PostMapping("createExcelByClass")
  @ApiOperation(value = "创建excel文件")
  public String createExcelByClass(@RequestBody List<Reimbursement> reimbursementList) {
    TFileEnclosureFtp fileEnclosureFtp = createExcelService.createExcelByClass(reimbursementList);
    return JSONObject.toJSONString(fileEnclosureFtp);
  }

  @PostMapping("createShenGongSiExcel")
  @ApiOperation(value = "创建省公司所需excel文件")
  public String createShenGongSiExcel(@RequestBody ReimbursementParam reimbursementParam) {
    TFileEnclosureFtp fileEnclosureFtp = createExcelService.createShenGongSiExcel(reimbursementParam.getPassbooks(),reimbursementParam.getArtificials());
    return JSONObject.toJSONString(fileEnclosureFtp);
  }
  
  @PostMapping("getWriteoffInstStatus")
  @ApiOperation(value = "获取报账单状态")
  public WriteoffInstStatusMessage getWriteoffInstStatus(@RequestBody List<GainWriteoffInstStatusItems> account) {
    return mssService.getWriteoffInstStatusBodyItem(account);
  }
  
  @PostMapping("createWriteoff")
  @ApiOperation(value = "员工医药费报账")
  public String medicineRetireAutoCreateWriteoff(@RequestBody String xml) {
    String postUrl = config.getEsb().getUrl() + config.getEsb().getApi().getAutoCreateWriteoff();
    return mssService.sendPost(xml, postUrl);
  }

  @PostMapping("test/{idNo}")
  @ApiOperation(value = "发送短信测试")
  public void getWriteoffInstStatus(@RequestBody List<String> witeoffids,@PathVariable String idNo) {
    uccpService.sendShortMessage(witeoffids,idNo);
  }

  @PostMapping("again")
  @ApiOperation(value = "重新发起报账测试")
  public void again(String id) {
    httpService.createWriteoffAgain(tWiteoffInstanceMapper.selectTwiteoffInstanceByOtherSystemMainId(id));
  }

  @PostMapping("getOpenWriteoffInstStatus")
  @ApiOperation(value = "更新非本系统发起的报账单状态")
  public void getOpenWriteoffInstStatus() {
    mssService.getOpenWriteoffInstStatus();
  }

  @PostMapping("testSum")
  @ApiOperation(value = "根据票据列表，校验该票据在所有地区可报销金额")
  public JsonResult<JSONObject> testSum(@RequestBody List<String> idList) {
    return JsonResult.success(wenZhouService.testSum(idList));
  }

//  @PostMapping("groupMembershipDuesAutoCreateWriteoff")
//  @ApiOperation(value = "社会团体会费报账")
//  public JsonResult<String> groupMembershipDuesAutoCreateWriteoff(@RequestBody EnvironmentalOrGroupMembershipDuesDTO duesDTO) {
//    return httpService.groupMembershipDuesAutoCreateWriteoff(duesDTO);
//  }
//
//  @PostMapping("environmentalAutoCreateWriteoff")
//  @ApiOperation(value = "环保费报账")
//  public JsonResult<String> environmentalAutoCreateWriteoff(@RequestBody EnvironmentalOrGroupMembershipDuesDTO duesDTO) throws IOException, UnirestException {
//    return httpService.environmentalAutoCreateWriteoff(duesDTO);
//  }
}
